package com.example.smartcity.ui;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.os.Build;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.smartcity.R;

import com.example.smartcity.adapter.MyFragmentApt;
import com.example.smartcity.bean.BannerBean;
import com.example.smartcity.bean.NewsClassify;
import com.example.smartcity.bean.NewsClassifyBean;
import com.example.smartcity.net.MyHttpCallBack;
import com.example.smartcity.net.OkhttpHelper;
import com.youth.banner.Banner;
import com.youth.banner.adapter.BannerImageAdapter;
import com.youth.banner.holder.BannerImageHolder;
import com.youth.banner.indicator.CircleIndicator;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * 指示器
 *
 * @author YSJ
 * @date 2021/4/22 : 21:00
 */
public class HomeActivity extends AppCompatActivity implements View.OnClickListener {

    private ArrayList<Fragment> fragments = new ArrayList<>();


    private MyFragmentApt apt;

    private ViewPager viewPage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        try {
            getBannerDate();
            getNewsClassify();
        } catch (Exception e) {
            e.printStackTrace();
        }


    }


    /**
     * 获取新闻分类
     */
    private void getNewsClassify() throws Exception {
        //http://124.93.196.45:10002/system/dict/data/type/press_category
        new OkhttpHelper().<NewsClassifyBean>get(
                null,
                "http://124.93.196.45:10002/system/dict/data/type/press_category",
                this,
                new MyHttpCallBack<NewsClassifyBean>() {
                    @Override
                    public void onResponse(NewsClassifyBean data, String json) {
                        initClassifyView(data);
                    }
                },
                NewsClassifyBean.class
        );
    }


    /**
     * 初始化分类
     *
     * @param data
     */

    TextView classify1;
    TextView classify2;
    TextView classify3;
    TextView classify4;
    TextView classify5;
    TextView classify6;

    private void initClassifyView(NewsClassifyBean data) {
        classify1 = findViewById(R.id.classify1);
        classify1.setText(data.getData().get(0).getDictLabel());

        classify2 = findViewById(R.id.classify2);
        classify2.setText(data.getData().get(1).getDictLabel());

        classify3 = findViewById(R.id.classify3);
        classify3.setText(data.getData().get(2).getDictLabel());

        classify4 = findViewById(R.id.classify4);
        classify4.setText(data.getData().get(3).getDictLabel());

        classify5 = findViewById(R.id.classify5);
        classify5.setText(data.getData().get(4).getDictLabel());

        classify6 = findViewById(R.id.classify6);
        classify6.setText(data.getData().get(5).getDictLabel());

        classify1.setOnClickListener(this);
        classify2.setOnClickListener(this);
        classify3.setOnClickListener(this);
        classify4.setOnClickListener(this);
        classify5.setOnClickListener(this);
        classify6.setOnClickListener(this);

        initViewPage(data);
    }


    /**
     * 初始化ViewPager
     *
     * @param data
     */

    private void initViewPage(NewsClassifyBean data) {
        for (NewsClassify datum : data.getData()) {
            fragments.add(new NewClassifyFragment(datum.getDictCode()));
        }

        apt = new MyFragmentApt(getSupportFragmentManager(), data, fragments);
        viewPage = findViewById(R.id.viewPage);
        viewPage.setAdapter(apt);

        viewPage.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                classify1.setTag(false);
                classify2.setTag(false);
                classify3.setTag(false);
                classify4.setTag(false);
                classify5.setTag(false);
                classify6.setTag(false);
                switch (position) {
                    case 0:
                        classify1.setTag(true);
                        break;
                    case 1:
                        classify2.setTag(true);
                        break;
                    case 2:
                        classify3.setTag(true);
                        break;
                    case 3:
                        classify4.setTag(true);
                        break;
                    case 4:
                        classify5.setTag(true);
                        break;
                    case 5:
                        classify6.setTag(true);
                        break;

                }
                upDateTextView();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }


    /**
     * 获取banner数据
     *
     * @throws Exception
     */
    private void getBannerDate() throws Exception {
        HashMap<String, String> map = new HashMap<>();
        map.put("pageNum", "1");
        map.put("pageSize", "10");
        map.put("type", "47");
        new OkhttpHelper().<BannerBean>get(
                map,
                "http://124.93.196.45:10002/userinfo/rotation/lists",
                this,
                new MyHttpCallBack<BannerBean>() {
                    @Override
                    public void onResponse(BannerBean data, String json) {
                        initBannerView(data);
                    }
                },
                BannerBean.class
        );
    }


    private void initBannerView(BannerBean data) {
        Banner banner = findViewById(R.id.mBanner);
        //BannerImageAdapter
        banner.setAdapter(new BannerImageAdapter<com.example.smartcity.bean.Banner>(data.getRows()) {  //设置适配器

            @Override
            public void onBindView(BannerImageHolder bannerImageHolder, com.example.smartcity.bean.Banner banner, int i, int i1) {
                //加载图片
                Glide.with(HomeActivity.this)
                        .load("http://124.93.196.45:10002" + banner.getImgUrl())
                        .into(bannerImageHolder.imageView);
            }
        }).setIndicator(new CircleIndicator(this)) //设置指示器
                .start(); //开始轮播
    }

    @Override
    public void onClick(View v) {
        classify1.setTag(false);
        classify2.setTag(false);
        classify3.setTag(false);
        classify4.setTag(false);
        classify5.setTag(false);
        classify6.setTag(false);

        switch (v.getId()) {
            case R.id.classify1:
                viewPage.setCurrentItem(0);
                break;
            case R.id.classify2:
                viewPage.setCurrentItem(1);
                break;
            case R.id.classify3:
                viewPage.setCurrentItem(2);
                break;
            case R.id.classify4:
                viewPage.setCurrentItem(3);
                break;
            case R.id.classify5:
                viewPage.setCurrentItem(4);
                break;
            case R.id.classify6:
                viewPage.setCurrentItem(5);
                break;
        }
        TextView v1 = (TextView) v;
        v1.setTag(true);
        upDateTextView();
    }

    private void upDateTextView() {
        Boolean tag = (Boolean) classify1.getTag();
        if (tag) {
            classify1.setTextSize(30);
        } else {
            classify1.setTextSize(20);
        }

        Boolean tag1 = (Boolean) classify2.getTag();
        if (tag1) {
            classify2.setTextSize(30);
        } else {
            classify2.setTextSize(20);
        }

        Boolean tag2 = (Boolean) classify3.getTag();
        if (tag2) {
            classify3.setTextSize(30);
        } else {
            classify3.setTextSize(20);
        }

        Boolean ta3 = (Boolean) classify4.getTag();
        if (ta3) {
            classify4.setTextSize(30);
        } else {
            classify4.setTextSize(20);
        }

        Boolean tag4 = (Boolean) classify5.getTag();
        if (tag4) {
            classify5.setTextSize(30);
        } else {
            classify5.setTextSize(20);
        }

        Boolean tag5 = (Boolean) classify6.getTag();
        if (tag5) {
            classify6.setTextSize(30);
        } else {
            classify6.setTextSize(20);
        }

    }
}